package com.gitee.sunstar.adblib

import android.util.Log

object ExLog {
    fun e(TAG: String, msg: String) {
        Log.e(TAG, "${getLogInfo()}$msg", )
    }

    fun i(TAG: String, msg: String) {
        Log.i(TAG, "${getLogInfo()}$msg", )
    }
    fun d(TAG: String, msg: String) {
        Log.d(TAG, "${getLogInfo()}$msg", )
    }
    fun w(TAG: String, msg: String) {
        Log.w(TAG, "${getLogInfo()}$msg", )
    }
    fun v(TAG: String, msg: String) {
        Log.v(TAG, "${getLogInfo()}$msg", )
    }


    private fun getLogInfo(): String? = kotlin.runCatching {
        val MATCH = "%s-%s-%s-%d"
        val CONNCTOR = ":"
        val stack = Thread.currentThread().stackTrace[4]
        val fileClassName = stack.className
        val info = kotlin.runCatching {
            String.format(
                MATCH,
                fileClassName.substring(0, fileClassName.indexOf(".")),
                stack.methodName,
                "[${Thread.currentThread().id}]",
                stack.lineNumber
            ) + CONNCTOR
        }.getOrElse { "" }
        info
    }.getOrElse {
        it.printStackTrace()
        ""
    }
}